/*
 * Created by JFormDesigner on Wed Dec 15 09:42:26 CST 2021
 */

package com.cn.项目.飞机预约管理系统.UserFrame;

import com.cn.项目.utils.JdbcUtils;
import com.cn.项目.飞机预约管理系统.Maininterface.Fail;
import com.cn.项目.飞机预约管理系统.Maininterface.Success;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;


public class BookTickets extends JFrame {
    public BookTickets() {
        initComponents();
    }

    String start = null;
    String end = null;
    String sTime = null;
    String eTime = null;
    String date = null;
    //预约监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        start = comboBox1.getSelectedItem().toString();
        end = comboBox2.getSelectedItem().toString();
        sTime = comboBox3.getSelectedItem().toString();
        eTime = comboBox4.getSelectedItem().toString();
        date = comboBox5.getSelectedItem().toString();
        String name = textField1.getText();
        String bname = textField2.getText(); //代买人
        String money = textField3.getText();

        Connection conn = null;
        PreparedStatement st = null;
        PreparedStatement st1 = null;
        PreparedStatement st2 = null;
        ResultSet rs = null;

        String nowMoney = null;
        try {
            conn = JdbcUtils.getConnection();


            String sql1="select *from users where uname=? ";
            st1 = conn.prepareStatement(sql1);
            st1.setString(1,bname);
            rs = st1.executeQuery();
            if(rs.next()){
               nowMoney = rs.getString("money");
            }
            if(Integer.parseInt(nowMoney)>=Integer.parseInt(money)){
                String n = String.valueOf(Integer.parseInt(nowMoney)-Integer.parseInt(money));
                String sql2="update users set money=? where uname=?";
                st2 = conn.prepareStatement(sql2);
                st2.setString(1, n);
                st2.setString(2,bname);
                int i = st2.executeUpdate();
                if (i>0){
                    System.out.println("更新成功！！");
                }else {
                    System.out.println("更新失败！！");
                }
                String sql = "insert into booktickets(`name`,bstart,bend,stime,etime,bdate,bname,bmoney)" +
                        "values (?,?,?,?,?,?,?,?)";
                st = conn.prepareStatement(sql);
                st.setString(1, name);
                st.setString(2, start);
                st.setString(3,end);
                st.setString(4,sTime );
                st.setString(5,eTime);
                st.setString(6, date);
                st.setString(7,bname);
                st.setString(8,money);
                int num = st.executeUpdate();
                if (num>0){
                    new Success();
                }else {
                    new Fail();
                }
            }else {
                new Tips();
            }


        } catch (SQLException e1) {
            e1.printStackTrace();
        }


    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        textField1 = new JTextField();
        label5 = new JLabel();
        comboBox1 = new JComboBox();
        comboBox2 = new JComboBox();
        comboBox3 = new JComboBox();
        comboBox4 = new JComboBox();
        textField2 = new JTextField();
        button1 = new JButton();
        label6 = new JLabel();
        comboBox5 = new JComboBox();
        label7 = new JLabel();
        textField3 = new JTextField();
        label8 = new JLabel();

        //======== this ========
        setVisible(true);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("预约机票");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 16f));
        label1.setForeground(new Color(0, 153, 153));
        contentPane.add(label1);
        label1.setBounds(130, 5, 230, 45);

        //---- label2 ----
        label2.setText("乘客姓名");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 6f));
        contentPane.add(label2);
        label2.setBounds(15, 70, 100, 35);

        //---- label3 ----
        label3.setText("选择航班：");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 6f));
        contentPane.add(label3);
        label3.setBounds(15, 130, 100, 35);

        //---- label4 ----
        label4.setText("航班时间：");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 6f));
        contentPane.add(label4);
        label4.setBounds(15, 190, 100, 35);
        contentPane.add(textField1);
        textField1.setBounds(125, 70, 180, 35);

        //---- label5 ----
        label5.setText("代买人");
        label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 6f));
        contentPane.add(label5);
        label5.setBounds(15, 295, 100, 35);
        contentPane.add(comboBox1);
        comboBox1.setBounds(125, 130, 115, 35);
        contentPane.add(comboBox2);
        comboBox2.setBounds(270, 130, 115, 35);
        contentPane.add(comboBox3);
        comboBox3.setBounds(125, 190, 115, 35);
        contentPane.add(comboBox4);
        comboBox4.setBounds(270, 190, 115, 35);
        contentPane.add(textField2);
        textField2.setBounds(125, 295, 125, 35);

        //---- button1 ----
        button1.setText("预约");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 6f));
        button1.setForeground(new Color(0, 204, 0));
        button1.addActionListener(e -> button1ActionPerformed(e));
        contentPane.add(button1);
        button1.setBounds(115, 420, 155, 40);

        //---- label6 ----
        label6.setText("航班日期：");
        label6.setFont(label6.getFont().deriveFont(label6.getFont().getSize() + 6f));
        contentPane.add(label6);
        label6.setBounds(15, 245, 100, 35);
        contentPane.add(comboBox5);
        comboBox5.setBounds(125, 245, 165, 35);

        //---- label7 ----
        label7.setText("价钱:");
        label7.setFont(label7.getFont().deriveFont(label7.getFont().getSize() + 6f));
        contentPane.add(label7);
        label7.setBounds(15, 355, 100, 35);
        contentPane.add(textField3);
        textField3.setBounds(125, 360, 110, 35);

        //---- label8 ----
        label8.setText("元");
        label8.setForeground(new Color(255, 51, 0));
        label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 6f));
        contentPane.add(label8);
        label8.setBounds(245, 360, 45, 35);

        contentPane.setPreferredSize(new Dimension(400, 500));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents

        comboBox1.addItem("上海");
        comboBox1.addItem("北京");
        comboBox1.addItem("广州");
        comboBox1.addItem("深圳");


        comboBox2.addItem("上海");
        comboBox2.addItem("洛杉矶");
        comboBox2.addItem("香港");
        comboBox2.addItem("濮阳");
        comboBox2.addItem("北京");
        comboBox2.addItem("澳门");


        comboBox3.addItem("8:00");
        comboBox3.addItem("10:00");
        comboBox3.addItem("12:00");
        comboBox3.addItem("14:00");
        comboBox3.addItem("16:00");
        comboBox3.addItem("18:00");

        comboBox4.addItem("8:00");
        comboBox4.addItem("10:00");
        comboBox4.addItem("12:00");
        comboBox4.addItem("14:00");
        comboBox4.addItem("16:00");
        comboBox4.addItem("18:00");


        comboBox5.addItem("2021-12-13");
        comboBox5.addItem("2021-12-14");
        comboBox5.addItem("2021-12-15");
        comboBox5.addItem("2021-12-16");
        comboBox5.addItem("2021-12-17");
        comboBox5.addItem("2021-12-18");
        comboBox5.addItem("2021-12-19");
        comboBox5.addItem("2021-12-20");


    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField1;
    private JLabel label5;
    private JComboBox comboBox1;
    private JComboBox comboBox2;
    private JComboBox comboBox3;
    private JComboBox comboBox4;
    private JTextField textField2;
    private JButton button1;
    private JLabel label6;
    private JComboBox comboBox5;
    private JLabel label7;
    private JTextField textField3;
    private JLabel label8;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

}
